{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1d3db6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write a number2\n",
      "0.6931471805599453\n",
      "write a number3\n",
      "1.0986122886681098\n",
      "write a number0\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "math domain error",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m input_number \u001b[38;5;241m==\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mq\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[0;32m      6\u001b[0m     beark\n\u001b[1;32m----> 7\u001b[0m result \u001b[38;5;241m=\u001b[39m math\u001b[38;5;241m.\u001b[39mlog(\u001b[38;5;28mfloat\u001b[39m(input_number))\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(result)\n",
      "\u001b[1;31mValueError\u001b[0m: math domain error"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for i in range(10):\n",
    "    input_number = input(\"write a number\")\n",
    "    if input_number =='q':\n",
    "        beark\n",
    "    result = math.log(float(input_number))\n",
    "    print(result)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "25d310be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write a number1\n",
      "0.0\n",
      "write a number0\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for i in range(10):\n",
    "    try:\n",
    "        input_number = input(\"write a number\")\n",
    "        if input_number =='q':\n",
    "            beark\n",
    "        result = math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError:\n",
    "        #print('ValueError: input must > 0')\n",
    "        break\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2027a9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write a number1\n",
      "ValueError: input must > 0\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for i in range(10):\n",
    "    try:\n",
    "        input_number = input(\"write a number\")\n",
    "        if input_number =='q':\n",
    "            beark\n",
    "        result = 1/math.log(float(input_number))\n",
    "        print(result)\n",
    "    except Exception:\n",
    "        print('ValueError: input must > 0')\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32b8704a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "write a number1\n",
      "log(value) must !=0\n",
      "write a number2\n",
      "1.4426950408889634\n",
      "write a numberq\n",
      "unknow error\n",
      "write a numberQ\n",
      "ValueError: input must > 0\n",
      "write a number2\n",
      "1.4426950408889634\n",
      "write a numberq\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for i in range(10):\n",
    "    \n",
    "    try:\n",
    "    \n",
    "        input_number = input(\"write a number\")\n",
    "        if input_number =='q':\n",
    "            beark\n",
    "        result = 1/math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError:\n",
    "        print('ValueError: input must > 0')\n",
    "    except ZeroDivisionError:\n",
    "        print('log(value) must !=0')\n",
    "    except Exception:\n",
    "        print('unknow error')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a90e9731",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tang\n",
      "tang2\n"
     ]
    },
    {
     "ename": "TangError",
     "evalue": "Invalid input: tang2",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTangError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m cur_input \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m()\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m cur_input \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m cur_list:\n\u001b[1;32m----> 7\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m TangError(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mInvalid input: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39mcur_input)\n",
      "\u001b[1;31mTangError\u001b[0m: Invalid input: tang2"
     ]
    }
   ],
   "source": [
    "class TangError(ValueError):\n",
    "    pass\n",
    "cur_list = ['tang','yu','di']\n",
    "while True:\n",
    "    cur_input = input()\n",
    "    if cur_input not in cur_list:\n",
    "        raise TangError('Invalid input: %s' %cur_input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "85b65f52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finally\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m----> 2\u001b[0m     \u001b[38;5;241m1\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m0\u001b[39m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfinally\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    1/0\n",
    "finally:\n",
    "    print('finally')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b010ae6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==0\n",
      "finally\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    1/0\n",
    "except:\n",
    "    print('==0')\n",
    "finally:\n",
    "    print('finally')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf385203",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
